package com.ldc.framework.activity

import android.content.Context
import android.content.Intent
import android.os.Message
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonlibs.uts.ApiUtils
import com.ldc.commonlibs.uts.MessageUts
import com.ldc.commonlibs.uts.ToastUtils
import com.ldc.commonlibs.uts.ViewModelUtils
import com.ldc.commonres.common.VBMActivity
import com.ldc.commonres.views.StateViewLayout
import com.ldc.framework.R
import com.ldc.framework.adapter.CoinAdapter
import com.ldc.framework.databinding.FrameworkActivityCoInBinding
import com.ldc.framework.viewmodel.FrameworkViewModelBase
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CoInActivity : VBMActivity<FrameworkActivityCoInBinding, FrameworkViewModelBase>() {
    private var page: Int = 1
    private var coinAdapter: CoinAdapter? = null

    companion object {
        private const val ARG_TITLE = "title"
        private const val REFRESH_CODE = 0x1231
        fun start(context: Context, title: String) {
            context.startActivity(Intent(context, CoInActivity::class.java).apply {
                putExtra(ARG_TITLE, title)
            })
        }

        fun start(context: Fragment, title: String) {
            start(context.requireContext(), title)
        }
    }

    override fun processMessage(msg: Message): Boolean {
        when (msg.what) {
            REFRESH_CODE -> {
                getCoinList()
            }
        }
        return super.processMessage(msg)
    }

    override fun initStateViewLayout(): StateViewLayout? =
        StateViewLayout.Companion.init(this, R.id.content_view)

    override fun main() {
        addOnReTryListener(object : OnRowListener<Int> {
            override fun onRow(v: View, data: Int) {
                page = 0
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }
        })
        initAdapter()
        viewBind?.topBar?.let {
            it.flBack.setOnClickListener { finish() }
            it.tvName.text = intent?.run {
                getStringExtra(ARG_TITLE)
            }
        }
        viewBind?.contentView?.refreshView?.setOnRefreshLoadMoreListener(object :
            OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page += 1
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }
        })
        MessageUts.sendMessage(getHandler(), REFRESH_CODE)
    }

    override fun createViewBinding(): FrameworkActivityCoInBinding =
        FrameworkActivityCoInBinding.inflate(layoutInflater)

    override fun createViewModel(): FrameworkViewModelBase =
        ViewModelUtils.create(this, FrameworkViewModelBase::class.java)


    private fun getCoinList() {
        showLoadingView()
        viewModel?.coinList(page)?.observe(this) {
            showContentView()
            viewBind?.contentView?.refreshView?.let { rf ->
                if (rf.state.isOpening) {
                    rf.finishRefresh()
                    rf.finishLoadMore()
                }
            }
            if (ApiUtils.isSuccess(it)) {
                if (page == 1) {
                    coinAdapter?.newData(it.data?.datas)
                } else coinAdapter?.addNewData(it.data?.datas)
            } else ToastUtils.showShort(it.errorMsg)
            if (coinAdapter?.isEmptyData()!!) {
                showEmptyView()
            }
        }
    }


    private fun initAdapter() {
        coinAdapter = CoinAdapter()
        viewBind?.contentView?.let { cv ->
            cv.rv.recyclerview.also { rv ->
                rv.adapter = coinAdapter
                rv.layoutManager =
                    LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
                rv.setHasFixedSize(true)

            }

        }
    }

}